package tests;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import src.BasicItem;
import src.Book;
import src.CD;
import src.GlobalConstants;
import src.Item;
import src.Magazine;
import src.Map;
import src.Video;

public class TestItem {

	private Item cd;

	private Item book;

	private Item map;

	private Item video;

	private Item magazine;

	@Before
	public void setUp() throws Exception {
		this.cd = new CD("CD", "authorCD", "01.01.1978");
		this.book = new Book("book", "authorbook", "01.01.1978", "1234567890");
		this.map = new Map("map", "authormap", "01.01.1978", "new foundland");
		this.video = new Video("video", "authorvideo", "01.01.1978");
		this.magazine = new Magazine("magazine", "authormagazine", "01.01.1978");
	}

	@After
	public void tearDown() throws Exception {
		this.cd = null;
		this.book = null;
		this.map = null;
		this.video = null;
		this.magazine = null;
		System.gc();
	}

	@Test
	public void testEquals() throws Exception {
		assertTrue("Books equals method does not work correctly!", book
				.equals(new Book("book", "authorbook", "01.01.1978",
						"1234567890")));
		assertTrue("Books equals method does not work correctly!", book
				.equals(book));
		assertFalse("Books equals method does not work correctly!", book
				.equals(cd));
		assertFalse("Books equals method does not work correctly!", book
				.equals(new Book("book2", "authorbook", "01.01.1978",
						"1234567890")));
		assertFalse("Books equals method does not work correctly!", book
				.equals(null));

		assertTrue("BasicItems equals method does not work correctly!", cd
				.equals(new CD("CD", "authorCD", "01.01.1978")));
		assertTrue("BasicItems equals method does not work correctly!", cd
				.equals(cd));
		assertFalse("BasicItems equals method does not work correctly!", cd
				.equals(magazine));
		assertFalse("BasicItems equals method does not work correctly!", cd
				.equals(new CD("CD2", "authorCD", "01.01.1978")));
		assertFalse("BasicItems equals method does not work correctly!", cd
				.equals(null));

		assertTrue("Maos equals method does not work correctly!", map
				.equals(new Map("map", "authormap", "01.01.1978",
						"new foundland")));
		assertTrue("Maps equals method does not work correctly!", map
				.equals(map));
		assertFalse("Maps equals method does not work correctly!", map
				.equals(video));
		assertFalse("Maos equals method does not work correctly!", map
				.equals(new Map("map2", "authormap", "01.01.1978",
						"new foundland")));
	}

	@SuppressWarnings("deprecation")
	@Test
	public void testGetFeePerDay() throws Exception {
		assertEquals("CD is set a wrong fee per day!", 4.0, cd.getFeePerDay());
		assertEquals("Book is set a wrong fee per day!", 1.0, book
				.getFeePerDay());
		assertEquals("Map is set a wrong fee per day!", 3.0, map.getFeePerDay());
		assertEquals("Video is set a wrong fee per day!", 5.0, video
				.getFeePerDay());
		assertEquals("Magazine is set a wrong fee per day!", 3.0, magazine
				.getFeePerDay());
	}

	@Test
	public void testGetMaxBorrowTime() throws Exception {
		assertEquals("CD is set a wrong borrow time!",
				GlobalConstants.MILLISECONDS_IN_ONE_DAY * 7L, cd
						.getMaxBorrowTime());
		assertEquals("Book is set a wrong borrow time!",
				GlobalConstants.MILLISECONDS_IN_ONE_DAY * 28L, book
						.getMaxBorrowTime());
		assertEquals("Map is set a wrong borrow time!",
				GlobalConstants.MILLISECONDS_IN_ONE_DAY * 21L, map
						.getMaxBorrowTime());
		assertEquals("Video is set a wrong borrow time!",
				GlobalConstants.MILLISECONDS_IN_ONE_DAY * 7L, video
						.getMaxBorrowTime());
		assertEquals("Magazine is set a wrong borrow time!",
				GlobalConstants.MILLISECONDS_IN_ONE_DAY * 14L, magazine
						.getMaxBorrowTime());
	}

	@Test
	public void testBasicItemGetter() throws Exception {
		assertEquals("Items do not have the correct publication date!",
				"01.01.1978", ((BasicItem) cd).getPublicationDate());
		assertEquals("Items do not have the correct title!", "CD",
				((BasicItem) cd).getTitle());
		assertEquals("Items do not have the correct author!", "authorCD",
				((BasicItem) cd).getAuthor());
	}

}
